// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 2008-9-8 17:54:16
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   t_role_EditMap.java

package com.creawor.hz_market.t_role;

import java.util.Iterator;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;

import com.creawor.hz_market.t_function.t_function;
import com.creawor.hz_market.t_role_function.t_role_function;
import com.creawor.imei.base.AbsEditMap;

// Referenced classes of package com.creawor.hz_market.t_role:
//            t_role, t_role_Form

public class t_role_EditMap extends AbsEditMap
{

    public t_role_EditMap()
    {
    }

    public void add(t_role_Form vo)
        throws HibernateException
    {
        t_role po = new t_role();
        try
        {
            po.setId(Integer.parseInt(vo.getId()));
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        try
        {
            po.setRole_name(vo.getRole_name());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        try
        {
            po.setDescription(vo.getDescription());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        Session session = beginTransaction();
        try
        {
            session.save(po);
            String[] functionids = vo.getFunctionid();
            save_t_role_functions(po, session, functionids);
            endTransaction(true);
        }
        catch(Exception e)
        {
            endTransaction(false);
            e.printStackTrace();
            throw new HibernateException(e);
        }
    }

    private void save_t_role_functions(t_role po, Session session,
			String[] functionids)  throws HibernateException
    {
    	for(int i = 0; i < functionids.length; i++)
            session.save(new t_role_function(po.getId(), Integer.parseInt(functionids[i])));

		
	}

	public void delete(t_role_Form vo)
        throws HibernateException
    {
        String id = vo.getId();
        delete(id);
    }

    public void delete(String id)
        throws HibernateException
    {
        Session session = beginTransaction();
        try
        {
            t_role po = (t_role)session.load(t_role.class, new Integer(id));
            session.delete(po);
            endTransaction(true);
        }
        catch(HibernateException e)
        {
            e.printStackTrace();
            endTransaction(false);
        }
    }

    public void update(t_role_Form vo)
        throws HibernateException
    {
        //System.out.println((new StringBuilder("upload t_role,id=")).append(vo.getId()).toString());
        
        Session session = beginTransaction();
        String id = vo.getId();
        t_role po = (t_role)session.load(t_role.class, new Integer(id));
        try
        {
            po.setId(Integer.parseInt(vo.getId()));
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        try
        {
            po.setRole_name(vo.getRole_name());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        try
        {
            po.setDescription(vo.getDescription());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        try
        {
            session.update(po);
            t_function function;
            for(Iterator it = po.getFunctions().iterator(); it.hasNext(); )
            {
            	
            	function = (t_function)it.next();
            	session.delete(new t_role_function(po.getId(), function.getId()));
            }
            
            save_t_role_functions(po, session, vo.getFunctionid());
            endTransaction(true);
        }
        catch(Exception e)
        {
            endTransaction(false);
            e.printStackTrace();
            throw new HibernateException(e);
        }
    }

    static Class class$0;
}